package com.halloon.android.view;

import java.text.DecimalFormat;

import com.actionbarsherlock.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

public class PorterDuffView extends ImageView{
	/**前景Bitmap高度为1像素,采用循环多次填充进度区域*/
    public static final int FG_HEIGHT = 1;
    /**下载进度前景色 */
    //public static final int FOREGROUND_COLOR = 0x77123456;
    public static final int FOREGROUND_COLOR = 0x77ff0000;
    /**下载进度条的颜色 */
    public static final int TEXT_COLOR = 0xff7fff00;
    /**进度版粉笔字体大小*/
    public static final int FONT_SIZE = 30;
    private Bitmap bitmapBg, bitmapFg;
    private Paint paint;
    /**标识当前进度*/
    private float progress;
    /**标识进度图片的宽度和高度*/
    private int width, height;
    /**格式化输出百分比*/
    private DecimalFormat decFormat;
    /**进度百分比文本的锚定Y中心坐标值 */
    private float txtBaseY;
    /**标识是否使用PorterDuff模式重组界面 */
    private boolean porterduffMode;
    /**标识是否正在下载图片 */
    private boolean loading;
    
    public PorterDuffView(Context context, AttributeSet attrs){
    	super(context, attrs);
    	init(context, attrs);
    }
    
    /**生成以宽与背景图片等同高为1像素的Bitmap */
    private static Bitmap createForegroundBitmap(int w){
    	Bitmap bm = Bitmap.createBitmap(w, FG_HEIGHT, Bitmap.Config.ARGB_8888);
    	Canvas c = new Canvas(bm);
    	Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
    	p.setColor(FOREGROUND_COLOR);
    	c.drawRect(0, 0, w, FG_HEIGHT, p);
    	
    	return bm;
    }
    
    private void init(Context context, AttributeSet attrs){
    	if(attrs != null){
    		//int count = attrs.getAttributeCount();
    		//for(int i = 0; i < count; i++){
    		//LogOut.out(this, "attrNameRes:" + 
    		//Inter.toHexString(attrs.getAttributeNameResource(i))
    		//+ " attrName:" + attrs.getAttributeName(i)
    		//+ " attrResValue:" + attrs.getAttributeResourceValue(i, -1)
    		//+ " attrValue:" + attrs.getAttributeValue(i)
    		//);
    		//}
    		/*
    		TypedArray typedArr = context.obtainStyledAttributes(attrs, R.styleable.porterduffView);
    		porterduffMode = typedArr.getBoolean(R.styleable.porterduff_PorterDuffView_porterduffmode, false); */
    	}
    	Drawable drawable = getDrawable();
    	if(porterduffMode && drawable != null && drawable instanceof BitmapDrawable){
    		bitmapBg = ((BitmapDrawable) drawable).getBitmap();
    		width = bitmapBg.getWidth();
    		height = bitmapBg.getHeight();
    		
    		bitmapFg = createForegroundBitmap(width);
    	}else{
    		porterduffMode = false;
    	}
    	
    	paint = new Paint();
    	paint.setFilterBitmap(false);
    	paint.setAntiAlias(true);
    	paint.setTextSize(FONT_SIZE);
    	
    	Paint.FontMetrics fontMetrics = paint.getFontMetrics();
    	
    	txtBaseY = (height - fontMetrics.bottom - fontMetrics.top) / 2;
    	
    	decFormat = new DecimalFormat("0.0%");
    }
    
    public void onDraw(Canvas canvas){
    	if(porterduffMode){
    		int tmpW = (getWidth() - width) / 2, tmpH = (getHeight() - height) / 2;
    		//画出背景图
    		canvas.drawBitmap(bitmapBg,  tmpW,  tmpH, paint);
    		//设置PorterDuff模式
    		paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DARKEN));
    		// canvas.drawBitmap(bitmapFg, tmpW, tmpH - progress * height, paint);
    		int tH = height - (int) (progress * height);
    		for(int i = 0; i < tH; i++){
    			canvas.drawBitmap(bitmapFg,  tmpW,  tmpH + i, paint);
    		}
    		
    		//立即取消xfermode
    		paint.setXfermode(null);
    		int oriColor = paint.getColor();
    		paint.setColor(TEXT_COLOR);
    		paint.setTextSize(FONT_SIZE);
    		String tmp = decFormat.format(progress);
    		float tmpWidth = paint.measureText(tmp);
    		canvas.drawText(decFormat.format(progress),  tmpW + (width - tmpWidth) / 2, tmpH + txtBaseY, paint);
    		//恢复为初始值时的颜色
    		paint.setColor(oriColor);
    	}else{
    		super.onDraw(canvas);
    	}
    }
    
    public void setProgress(float progress){
    	if(porterduffMode){
    		this.progress = progress;
    		invalidate();
    	}
    }
    
    public void setBitmap(Bitmap bg){
    	if(porterduffMode){
    		bitmapBg = bg;
    		width = bitmapBg.getWidth();
    		height = bitmapBg.getHeight();
    		
    		bitmapFg = createForegroundBitmap(width);
    		
    		Paint.FontMetrics fontMetrics = paint.getFontMetrics();
    		txtBaseY = (height - fontMetrics.bottom - fontMetrics.top) / 2;
    		
    		setImageBitmap(bg);
    		//请求重新布局,将会再次调用onMeasure();
    		//requestLayout();
    	}
    }
    
    public boolean isLoading(){
    	return loading;
    }
    
    public void setLoading(boolean loading){
    	this.loading = loading;
    }
    
    public void setPorterDuffMode(boolean bool){
    	porterduffMode = bool;
    }
    
}
